using System.IO;
using System.Resources;
using ResxHelper.Object;

namespace ResxHelper.Logic.Writers
{
    public class ResourceFileResxFormatter : IResourceFileFormatter
    {

        public ResourceFile InputFile { get; private set; }

        public ResourceFileResxFormatter(ResourceFile input)
        {
            InputFile = input;
        }

        public string Run()
        {
            using (var stream = new MemoryStream())
            {
                using (var resx = new ResXResourceWriter(stream))
                {
                    // build the resx and write to memory
                    foreach (var entry in InputFile.Entries.Values)
                    {
                        resx.AddResource(new ResXDataNode(entry.Key, entry.Value) { Comment = entry.Comment });
                    }
                    resx.Generate();
                    stream.Position = 0;
                    using (var reader = new StreamReader(stream))
                    {
                        var text = reader.ReadToEnd();
                        return text;
                    }
                }
            }
        }
    }
}
